﻿#include "MyMainRole.h"

//移动时保存地址
MyMainRole *moveRole;

MyMainRole::MyMainRole(qreal mustWidth,int state)
{
    //setFlags(QGraphicsItem::ItemIsSelectable);
    if(state == 0)
        setZValue(3);
    else if (state == 2) {
        setZValue(1);
    }
    else
    setZValue(2);
    mWidth = mustWidth;
    mState = state;

    list.append(QPixmap(":/pixmap/PNG/Default size/Player/player_05.png"));
    list.append(QPixmap(":/pixmap/PNG/Default size/Crates/crate_31.png"));
    list.append(QPixmap(":/pixmap/PNG/Default size/Crates/crate_10.png"));
    list.append(QPixmap(":/pixmap/PNG/Default size/Blocks/block_07.png"));
    setState(mState);
    hide();

    if(state == 0)
    {//上下左右
        mainRoleList.append(QPixmap(":/pixmap/PNG/Default size/Player/player_08.png"));
        mainRoleList.append(QPixmap(":/pixmap/PNG/Default size/Player/player_05.png"));
        mainRoleList.append(QPixmap(":/pixmap/PNG/Default size/Player/player_14.png"));
        mainRoleList.append(QPixmap(":/pixmap/PNG/Default size/Player/player_11.png"));
    }

    //启用拖动事件
//    this->setAcceptDrops(true);
}

MyMainRole::~MyMainRole()
{
    qDebug() << "~MyMainRole()";
}

void MyMainRole::setPoint(int x, int y)
{
    mPoint = QPoint(x,y);
}

//变成json数据
QJsonObject MyMainRole::toJson()
{
    QJsonObject obj;
    obj.insert("mrole",mState);
    obj.insert("mx",mPoint.x());
    obj.insert("my",mPoint.y());
    return obj;
}

int MyMainRole::getState()
{
    return mState;
}

void MyMainRole::setTowards(int towards)
{
    if(mState != 0)
        return;

    if((towards<0)||(towards>=mainRoleList.count()))
    {
        qWarning("MyMainRole::setMainRoleTowards(int state)不正常");
        return;
    }
    this->setPixmap(mainRoleList[towards]);
    //int mapw = mainRoleList[towards].height();
    //this->setScale(mWidth/mapw);
}

QPoint MyMainRole::getPoint()
{
    return mPoint;
}

void MyMainRole::setState(int state)
{
    if((state<0)||(state>=list.count()))
    {
        qWarning("MyMainRole::setState(int state)不正常");
        return;
    }
    this->setPixmap(list[state]);
    int mapw = list[state].height();
    this->setScale(mWidth/mapw);
}

void MyMainRole::mousePressEvent(QGraphicsSceneMouseEvent *event)
{
//    if(event->button() == Qt::LeftButton)
//    {
//        emit sendGarbageDisplay(true);

//        QDrag* drag = new QDrag(this);
//        QMimeData* mindata = new QMimeData();
//        QString text = "move";
//        moveRole = this;
//        mindata->setText(text);
//        drag->setMimeData(mindata);
//        drag->exec();

//        emit sendGarbageDisplay(false);
//    }
}

